package com.eeepay.eeepay_v2.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.DataDetailsActivity;
import com.eeepay.eeepay_v2.adapter.PaperAdapter;
import com.eeepay.eeepay_v2.adapter.PaperQueryAdapter;
import com.eeepay.eeepay_v2.model.PapersInfo;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.eeepay_v2.view.ScrollGridView;
import com.eeepay.v2_library.view.HorizontalItemView;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.LeftRightText;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.eeepay.api.grpc.nano.MerAddModular;
import cn.eeepay.api.grpc.nano.MerAddServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/6/30.
 * 进件查询其他信息界面
 */
public class OtherInfoFragment extends ABBaseFragment {

    private static OtherInfoFragment fragment;
    private View view;
    private ScrollGridView paperGv;
    private LabelEditText lawerLet, idLet, merNameLet, addressLet, accNameLet, accNoLet;
    private LeftRightText accBankLrt, zhLrt, zhAreaLrt;
    private HorizontalItemView areaHiv, merTypeHiv, induHiv;
    private RadioGroup accTypeRg;

    private PaperQueryAdapter adapter;
    private MerAddModular.MerRequireItem item;
    private MerAddModular.MerRequireItem[] photo;
    private MerAddModular.MerRequireItem[] otherInfo;
    private MerAddModular.MerchantInfo merInfo;
    //    private List<PapersInfo> paperList = new ArrayList<>();
//    private String bpId, merNo;//业务产品和商户ID

//    public static OtherInfoFragment getInstance(MerAddModular.MerRequireItem[] photo,
//                                                MerAddModular.MerRequireItem[] otherInfo,
//                                                MerAddModular.MerchantInfo merInfo) {
//        if (fragment == null) {
//            synchronized (OtherInfoFragment.class) {
//                if (fragment == null) {
//                    fragment = new OtherInfoFragment(photo,otherInfo,merInfo);
//                }
//            }
//        }
//        return fragment;
//    }

    public OtherInfoFragment() {
    }

//    public OtherInfoFragment(MerAddModular.MerRequireItem[] photo,
//                             MerAddModular.MerRequireItem[] otherInfo,
//                             MerAddModular.MerchantInfo merInfo) {
//        this.photo = photo;
//        this.otherInfo = otherInfo;
//        this.merInfo = merInfo;
//    }


//    public OtherInfoFragment(String bpId, String merNo) {
//        this.bpId = bpId;
//        this.merNo = merNo;
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_others_info, container, false);
        initView();

        initData();
        return view;
    }

    private void initView() {
        paperGv = (ScrollGridView) view.findViewById(R.id.gv_papers);
        lawerLet = (LabelEditText) view.findViewById(R.id.let_legal_person);
        idLet = (LabelEditText) view.findViewById(R.id.let_legal_id);
        merNameLet = (LabelEditText) view.findViewById(R.id.let_bisInfo_merchant_name);
        addressLet = (LabelEditText) view.findViewById(R.id.let_bisInfo_add_detail);
        accNameLet = (LabelEditText) view.findViewById(R.id.let_settle_account_name);
        accNoLet = (LabelEditText) view.findViewById(R.id.let_settle_account_number);
        accBankLrt = (LeftRightText) view.findViewById(R.id.lrt_settle_account_bank);
        accBankLrt.setVisibility(View.GONE);
        zhLrt = (LeftRightText) view.findViewById(R.id.lrt_settle_account_zh);
        zhLrt.getRTextView().setLines(2);
        zhAreaLrt = (LeftRightText) view.findViewById(R.id.lrt_settle_account_area);
//        unitedLet = (LabelEditText) view.findViewById(R.id.let_settle_united_bank_number);

        areaHiv = (HorizontalItemView) view.findViewById(R.id.hiv_bisInfo_area);
        merTypeHiv = (HorizontalItemView) view.findViewById(R.id.hiv_bisInfo_merchant_type);
        induHiv = (HorizontalItemView) view.findViewById(R.id.hiv_bisInfo_merchant_industry);

        accTypeRg = (RadioGroup) view.findViewById(R.id.rgp_into_account_type);
    }

    private void initData() {
        Log.d("ching", "OtherInfoFragment");
        photo = ((DataDetailsActivity) getActivity()).getPhoto();
        merInfo = ((DataDetailsActivity) getActivity()).getMerInfo();
        otherInfo = ((DataDetailsActivity) getActivity()).getOther();
//        sendHttpRequest(Constant.RequestCode.PAPERS);
//        sendHttpRequest(Constant.RequestCode.BASIC_DATA);
        if (photo != null) {
            Arrays.sort(photo, new Comparator<MerAddModular.MerRequireItem>() {
                @Override
                public int compare(MerAddModular.MerRequireItem lhs, MerAddModular.MerRequireItem rhs) {
                    Integer item1 = Integer.valueOf(lhs.mriId);
                    Integer item2 = Integer.valueOf(rhs.mriId);
                    return item1.compareTo(item2);
                }
            });
            adapter = new PaperQueryAdapter(getActivity());
            adapter.addAll(Arrays.asList(photo));
        }

        if (merInfo != null) {
            lawerLet.setEditContent(merInfo.lawyer);//法人姓名
            lawerLet.setEnableEdit(false);
            merNameLet.setEditContent(merInfo.merchantName);//商户名称
            merNameLet.setEnableEdit(false);
            merTypeHiv.setRightText(merInfo.businessType);//商户类别
            merTypeHiv.showRightDraw(false);
            induHiv.setRightText(merInfo.industryType);//所处行业
        }

        if (otherInfo != null) {
            for (int i = 0; i < otherInfo.length; i++) {
                Log.d("ching", " id: " + otherInfo[i] + " | content: " + otherInfo[i].content);
                item = otherInfo[i];
                switch (item.mriId) {
                    case "1":
                        switch (item.content) {
                            case "对私"://账户类型
                                accTypeRg.getChildAt(0).performClick();
                                accTypeRg.getChildAt(1).setEnabled(false);
                                break;
                            case "对公":
                                accTypeRg.getChildAt(1).performClick();
                                accTypeRg.getChildAt(0).setEnabled(false);
                                break;
                        }
                        break;
                    case "2"://开户名
                        accNameLet.setEditContent(item.content);
                        accNameLet.setEnableEdit(false);
                        break;
                    case "3"://开户账号
                        if (!TextUtils.isEmpty(item.content)){
                            String accStr = "************"+item.content.substring(item.content.length()-4);
                            accNoLet.setEditContent(accStr);
                            accNoLet.setEnableEdit(false);
                        }
                        break;
                    case "4"://开户银行
//                        accBankLet.setEditContent(item.content);
//                        accBankLet.setEnableEdit(false);
                        zhLrt.setRightText(item.content);
                        break;
                    case "5"://联行行号
//                        unitedLet.setEditContent(item.content);
//                        unitedLet.setEnableEdit(false);
                        break;
                    case "6"://身份证号
                        if (!TextUtils.isEmpty(item.content)) {
                            String idNo = "**************"+item.content.substring(item.content.length()-4);
                            idLet.setEditContent(idNo);
                            idLet.setEnableEdit(false);
                        }
                        break;
                    case "7"://地址
                        String add[] = item.content.split("-");
                        if (add.length > 1) {
                            areaHiv.setRightText(add[0] + add[1]);
                            addressLet.setEditContent(add[2]);
                        } else {
                            areaHiv.setRightText(add[0]);
                            addressLet.setEditContent(add[1]);
                        }
                        areaHiv.showRightDraw(false);
//                        addressLet.setEditContent(add[1]);
                        addressLet.setEnableEdit(false);
                        break;
                    case "15"://开户行地区
                        zhAreaLrt.setRightText(item.content);
                        break;
                }
            }
        }
        paperGv.setAdapter(adapter);
    }

//    @Override
//    protected void sendHttpRequest(int taskId) {
//        showProgressDialog(Constant.TIP_LOADING);
//        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT
//                , taskId, new GrpcUtils.AsyncTaskListener() {
//                    @Override
//                    public Object doInBack(ManagedChannel channel, int taskId) {
//                        MerAddServiceGrpc.MerAddServiceBlockingStub stub = MerAddServiceGrpc.newBlockingStub(channel);
//                        MerAddModular.QueryMerInfoReq queryReq = new MerAddModular.QueryMerInfoReq();
//                        UserInfo userInfo = UserInfo.getInstance();
//
//                        queryReq.oneAgentNo = userInfo.getOneAgentNo();
//                        queryReq.parentNode = userInfo.getAgentNode();
//                        Log.d("saki", " bpId " + bpId + " || merchantNo" + merNo);
//                        queryReq.bpId = bpId;
//                        queryReq.merchantNo = merNo;
//                        switch (taskId) {
//                            case Constant.RequestCode.PAPERS:
//                                return stub.queryMerRequireItem(queryReq);
//                            case Constant.RequestCode.BASIC_DATA:
//                                return stub.queryMerCardInfo(queryReq);
//                        }
//                        return null;
//                    }
//
//                    @Override
//                    public void updateUI(Object result, int taskId) {
//                        dismissProgressDialog();
//
//                        switch (taskId) {
//                            case Constant.RequestCode.PAPERS:
//                                if (result == null) {
//                                    showToast("获取证件资料异常");
//                                    return;
//                                }
//                                MerAddModular.MerRequireItemList itemResponse = (MerAddModular.MerRequireItemList) result;
//                                if (itemResponse.resMsg.status) {//成功
//                                    MerAddModular.MerRequireItem[] datas = itemResponse.merRequireItemList;
//                                    for (int i = 0; i < datas.length; i++) {
//                                        Log.d("other", "排序前：mriId >>>" + datas[i].mriId
//                                                        + " ||  id>>>" + datas[i].id
//// + " ||  content>>>" + datas[i].content
////                                                + " ||  merchantNo +" + datas[i].merchantNo
//                                        );
//                                    }
//                                    Arrays.sort(datas, new Comparator<MerAddModular.MerRequireItem>() {
//                                        @Override
//                                        public int compare(MerAddModular.MerRequireItem lhs, MerAddModular.MerRequireItem rhs) {
//                                            Integer item1 = Integer.valueOf(lhs.mriId);
//                                            Integer item2 = Integer.valueOf(rhs.mriId);
//                                            return item1.compareTo(item2);
//                                        }
//                                    });
//                                    for (int i = 0; i < datas.length; i++) {
//                                        Log.d("other", "排序后：mriId >>>" + datas[i].mriId
//                                                + " ||  id>>>" + datas[i].id + " ||  content>>>" + datas[i].content
//                                                + " ||  merchantNo +" + datas[i].merchantNo
//                                        );
//                                    }
//                                    adapter.addAll(Arrays.asList(datas));
//                                } else {
//                                    showToast(itemResponse.resMsg.msg);
//                                }
//                                break;
//                            case Constant.RequestCode.BASIC_DATA:
//                                if (result == null) {
//                                    showToast("获取其他信息异常");
//                                    return;
//                                }
//
//                                MerAddModular.MerServiceCardInfo infoResponse = (MerAddModular.MerServiceCardInfo) result;
////                                Log.d("other", "status-->" + infoResponse.resMsg.status);
//                                if (infoResponse.resMsg.status) {//成功
//                                    MerAddModular.MerchantInfo merInfo = infoResponse.merInfo;
//                                    lawerLet.setEditContent(merInfo.lawyer);//法人姓名
//                                    lawerLet.setEnableEdit(false);
//                                    merNameLet.setEditContent(merInfo.merchantName);//商户名称
//                                    merNameLet.setEnableEdit(false);
//                                    merTypeHiv.setRightText(merInfo.businessType);//商户类别
//                                    merTypeHiv.showRightDraw(false);
//                                    induHiv.setRightText(merInfo.industryType);//所处行业
//
//                                    MerAddModular.MerRequireItem[] itemList = infoResponse.merRequireItemList;
//                                    MerAddModular.MerRequireItem item;
//                                    Log.d("other", "length-->" + itemList.length);
//                                    for (int i = 0; i < itemList.length; i++) {
//                                        Log.d("other", "mriId >>>" + itemList[i].mriId
//                                                + " ||  id>>>" + itemList[i].id + " ||  content>>>" + itemList[i].content
//                                                + " ||  merchantNo +" + itemList[i].merchantNo
//                                        );
//                                        item = itemList[i];
//                                        switch (item.mriId) {
//                                            case "1":
//                                                switch (item.content) {
//                                                    case "对私"://账户类型
//                                                        accTypeRg.getChildAt(0).performClick();
//                                                        accTypeRg.getChildAt(1).setEnabled(false);
//                                                        break;
//                                                    case "对公":
//                                                        accTypeRg.getChildAt(1).performClick();
//                                                        accTypeRg.getChildAt(0).setEnabled(false);
//                                                        break;
//                                                }
//                                                break;
//                                            case "2"://开户名
//                                                accNameLet.setEditContent(item.content);
//                                                accNameLet.setEnableEdit(false);
//                                                break;
//                                            case "3"://开户账号
//                                                accNoLet.setEditContent(item.content);
//                                                accNoLet.setEnableEdit(false);
//                                                break;
//                                            case "4"://开户银行
//                                                accBankLet.setEditContent(item.content);
//                                                accBankLet.setEnableEdit(false);
//                                                break;
//                                            case "5"://联行行号
//                                                unitedLet.setEditContent(item.content);
//                                                unitedLet.setEnableEdit(false);
//                                                break;
//                                            case "6"://身份证号
//                                                idLet.setEditContent(item.content);
//                                                idLet.setEnableEdit(false);
//                                                break;
//                                            case "7"://地址
//                                                String add[] = item.content.split("-");
//                                                areaHiv.setRightText(add[0]);
//                                                areaHiv.showRightDraw(false);
////                                                if (add[2]!=null){
////                                                    addressLet.setEditContent(add[1] + add[2]);
////                                                } else {
//                                                addressLet.setEditContent(add[1]);
////                                                }
////                                                addressLet.setEditContent(item.content);
//                                                addressLet.setEnableEdit(false);
//                                                break;
//                                        }
//                                    }
//                                } else {
//                                    showToast(infoResponse.resMsg.msg);
//                                }
//                                break;
//                        }
//                    }
//                });
//    }
}
